package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.lxj.xpopup.XPopup
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityWithdrawBinding
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.ui.newpage.adapter.WithDrawContentAdapter
import com.starsea.book.ui.newpage.pop.WithdrawDealPop
import com.starsea.book.ui.newpage.utils.finialDouble
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-03-28 18 GMT+8
 * @desc :
 */
class WithdrawalActivity : VMBaseActivity<ActivityWithdrawBinding, WalletViewModel>() {
    override val binding by viewBinding(ActivityWithdrawBinding::inflate)
    override val viewModel by viewModels<WalletViewModel>()

    lateinit var mAdapter: WithDrawContentAdapter

    var cash = ""


    override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.account_manage, menu)
        return super.onCompatCreateOptionsMenu(menu)
    }


    override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_share_it -> {
                startActivity<AccountManageActivity> { }
            }

        }
        return super.onCompatOptionsItemSelected(item)
    }

    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        viewModel.getWalletInfo()
        viewModel.getWithdrawList()

        mAdapter = WithDrawContentAdapter(this) {
            cash = (it.amount ?: "").toDouble().finialDouble()
            viewModel.withdraw(it.ruleId ?: "")
        }

        binding.rvCash.layoutManager = GridLayoutManager(this, 2)
        binding.rvCash.adapter = mAdapter


        viewModel.walletInfoResult.observe(this) { data ->
            if (data.status == 200) {

                binding.tvNumber.text = (data.data!!.rmbBalance ?: "0").toDouble().finialDouble()

            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }

        viewModel.withdrawListResult.observe(this) { data ->
            if (data.status == 200) {
                mAdapter.setItems(data.data)
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }

        viewModel.withdrawResult.observe(this) { data ->
            if (data.status == 200) {
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asCustom(WithdrawDealPop(this, cash))
                    .show()
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


    }
}